<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Document</title>
    <style></style>
  </head>
  <body>
    <div>
      <h1>减少循环体中活动</h1>
      <p>可以提取不必要的代码到循环体外执行(经常使用且不变的值)</p>
    </div>
    <script>
      var test = () => {
        var i;
        var arr = ["xjq", 22, "男"];
        for (i = 0; i < arr.length; i++) {
          console.log(arr[i]);
        }
      };

      //优化
      var test = () => {
        var i;
        var arr = ["xjq", 22, "男"];
        var len = arr.length;
        for (i = 0; i < len; i++) {
          console.log(arr[i]);
        }
      };

      //while循环----while优于for--因为while是从后往前
      var test = () => {
        var arr = ["xjq", 22, "男"];
        var len = arr.length;
        while (len--) {
          console.log(arr[len]);
        }
      };
    </script>
  </body>
</html>
